unit FMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Mask, RXSpin, frxClass, DB, ADODB, RxMemDS,
  Grids, DBGrids, frxDBSet, DBClient, frxBarcode, StrUtils, inifiles,
  frxDesgn;

type
  TFormMain = class(TForm)
    lTitulo: TLabel;
    gbPosicion: TGroupBox;
    Horizonal: TLabel;
    eHorizontal: TRxSpinEdit;
    Label2: TLabel;
    eVertical: TRxSpinEdit;
    gbNumeracion: TGroupBox;
    Label3: TLabel;
    eNumIni: TEdit;
    Label4: TLabel;
    eNumFin: TEdit;
    BtnImprimir: TBitBtn;
    frxDBDatos: TfrxDBDataset;
    TablaCSV: TADOTable;
    frxReport: TfrxReport;
    Label6: TLabel;
    cbDocs_x_hoja: TComboBox;
    gbLog: TGroupBox;
    mLog: TMemo;
    frxDesigner: TfrxDesigner;
    procedure BtnImprimirClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure cbDocs_x_hojaChange(Sender: TObject);
    procedure lTituloDblClick(Sender: TObject);
  private
    { Private declarations }
    Function GenerarDato( Numero, Ult_Num: Integer): String;
    Procedure GenerarNumeros_2;
    Procedure GenerarNumeros_4;
    Procedure Log;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.BtnImprimirClick(Sender: TObject);
var
  i   : Integer;
  obj : TComponent;
begin
  TablaCSV.Close;
  if cbDocs_x_hoja.ItemIndex = 0 then
  begin
    GenerarNumeros_2;
    frxReport.LoadFromFile( 'rep_MultasAytoMurcia_2.fr3');
  end
  else
    begin
      GenerarNumeros_4;
      frxReport.LoadFromFile( 'rep_MultasAytoMurcia_4.fr3');
    end;

  for i:=0 to frxReport.ComponentCount-1 do
  begin
    obj:= frxreport.Components[i]; // find the page
    if obj.ClassName = 'TfrxMemoView' then
    begin
      TfrxMemoView( Obj).Top  := TfrxMemoView( Obj).Top  + eVertical.Value * 3.78;
      TfrxMemoView( Obj).Left := TfrxMemoView( Obj).Left + eHorizontal.Value * 3.78;
    end
    else if obj.ClassName = 'TfrxBarCodeView' then
    begin
      TfrxBarCodeView( Obj).Top  := TfrxBarCodeView( Obj).Top  + eVertical.Value * 3.78;
      TfrxBarCodeView( Obj).Left := TfrxBarCodeView( Obj).Left + eHorizontal.Value * 3.78;
    end;
  end;
  frxReport.PrepareReport();
  Log;
  frxReport.Print;
end;


procedure TFormMain.GenerarNumeros_2;
var
  Num_Ini,
  Num_Fin,
  Cant   ,
  Numero : LongInt;
  Linea  : String;
  Fich   : TextFile;
begin
  AssignFile( Fich, 'datos.csv');
  Rewrite( Fich);
  Num_Ini := StrToInt( eNumIni.Text);
  Num_Fin := StrToInt( eNumFin.Text);
  Cant    := ( StrToInt( eNumFin.Text) - Num_Ini) div 2;
  For Numero := Num_Ini to Num_Ini + Cant do
  begin
    Linea := GenerarDato( Numero, Num_Fin) +
             GenerarDato( Numero + Cant + 1, Num_Fin) ;
    Writeln( Fich, Linea);
  end;
  Flush( Fich);
  CloseFile( Fich);
end;



procedure TFormMain.GenerarNumeros_4;
var
  Num_Ini,
  Num_Fin,
  Cant   ,
  Numero : LongInt;
  Linea  : String;
  Fich   : TextFile;
begin
  AssignFile( Fich, 'datos.csv');
  Rewrite( Fich);
  Num_Ini := StrToInt( eNumIni.Text);
  Num_Fin := StrToInt( eNumFin.Text);
  Cant    := ( StrToInt( eNumFin.Text) - Num_Ini) div 4;
  For Numero := Num_Ini to Num_Ini + Cant do
  begin
    Linea := GenerarDato( Numero               , Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 1, Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 2, Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 3, Num_Fin) ;
    Writeln( Fich, Linea);
  end;
  Flush( Fich);
  CloseFile( Fich);
end;



procedure TFormMain.Log;
begin
  mLog.Lines.Insert( 0, FormatDateTime( 'dd/mm/yyyy hh:nn:ss', now) + ' --> ' + FormatFloat( '#,##0', StrToInt( eNumIni.Text)) + ' - ' + FormatFloat( '#,##0', StrToInt( eNumFin.Text)) + ' (' + cbDocs_x_hoja.Text + ')');
end;

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
  Ini: TIniFile;
  Sec: String;
begin
  Sec := 'Config_' + cbDocs_x_hoja.Text;
  Ini := TIniFile.Create( ExtractFilePath( Application.ExeName) + changeFileExt( ExtractFileName( Application.ExeName),'.ini'));
  try
    Ini.WriteString( Sec, 'Desp_X' , eHorizontal.Text);
    Ini.WriteString( Sec, 'Desp_Y' , eVertical.Text);
    Ini.WriteString( Sec, 'Num_Ini', eNumIni.Text);
    Ini.WriteString( Sec, 'Num_Fin', eNumFin.Text);
    Ini.WriteInteger( 'Cfg', 'Tipo_Imp', cbDocs_x_hoja.ItemIndex);
  finally
    Ini.Free;
  end;
end;

procedure TFormMain.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create( ExtractFilePath( Application.ExeName) + changeFileExt( ExtractFileName( Application.ExeName),'.ini'));
  try
    cbDocs_x_hoja.ItemIndex := Ini.ReadInteger( 'Cfg', 'Tipo_Imp', 0);
  finally
    Ini.Free;
  end;
  cbDocs_x_hojaChange( Sender);
end;

procedure TFormMain.cbDocs_x_hojaChange(Sender: TObject);
var
  Ini: TIniFile;
  Sec: String;
begin
  Sec := 'Config_' + cbDocs_x_hoja.Text;
  Ini := TIniFile.Create( ExtractFilePath( Application.ExeName) + changeFileExt( ExtractFileName( Application.ExeName),'.ini'));
  try
    eHorizontal.Text := Ini.ReadString( Sec, 'Desp_X' , '0');
    eVertical.Text   := Ini.ReadString( Sec, 'Desp_Y' , '0');
    eNumIni.Text     := Ini.ReadString( Sec, 'Num_Ini', '0');
    eNumFin.Text     := Ini.ReadString( Sec, 'Num_Fin', '0');
  finally
    Ini.Free;
  end;
end;

procedure TFormMain.lTituloDblClick(Sender: TObject);
begin
  if cbDocs_x_hoja.ItemIndex = 0 then
    frxReport.LoadFromFile( 'rep_MultasAytoMurcia_2.fr3')
  else
    frxReport.LoadFromFile( 'rep_MultasAytoMurcia_4.fr3');
  frxReport.DesignReport;
end;


function TFormMain.GenerarDato( Numero, Ult_Num: Integer): String;

  function DC( Numero: Integer): String;
  var
    Num: Integer;
  begin
    Num  := StrToInt64( '040013' + FormatFloat( '000000', Numero) + '300305') mod 7;
    if Num = 0 then Num := 7;
    Result := IntToStr( Num)
  end;

var
  digito: String;
begin
  if Numero <= Ult_Num then
  begin
    digito := DC( Numero);
    Result := '90523300305' + '040013' + FormatFloat( '000000', Numero) + Digito +
              '040 0 13' + FormatFloat( '000000', Numero) + ' ' + Digito + '         '
  end
  else
    Result := DupeString( ' ', 50);
end;

end.
